package model
{
	public class Stack
	{
		private var top:StackNode;
		private var numElements:int;
		
		public function Stack()
		{
			top = null;
			numElements = 0;
		}
		
		public function push(obj:Object):void
		{
			var newNode:StackNode = new StackNode(obj);
			
			if(top == null)
				top = newNode;
			else
			{
				newNode.previous = top;
				top = newNode;
			}
			
			numElements++;
		}
		 
		public function pop():Object
		{
			if(top != null)
			{
				var result:Object = top.value;
				top = top.previous;
				
				numElements--;
				
				return result;
			}
			else
				return null;
		}
		 
		public function peek():Object
		{
			if (top != null)
				return top.value;
			else
				return null;
		}
		
		public function randomSwap():void
		{
			var indexA:int = int(Math.random() * numElements);
			var indexB:int = int(Math.random() * numElements);
			var i:int;
			
			var auxObjectA:StackNode = top;
			var auxObjectB:StackNode = top;
			
			var valueTemp:Object;
			
			for (i=0; i<indexA; i++) auxObjectA = auxObjectA.previous;
			for (i=0; i<indexB; i++) auxObjectB = auxObjectB.previous;
			
			valueTemp = auxObjectA.value;
			auxObjectA.value = auxObjectB.value;
			auxObjectB.value = valueTemp;
		}
	}
}